﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ControlLayer;

namespace SEMAuto
{
    public partial class LapConfiguration : Form
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public LapConfiguration(CarController controller)
        {
            InitializeComponent();
            //For handling keyboard buttons
            this.KeyPreview = true;
            _carcontroller = controller;
        }

        #region Fields
        private CarController _carcontroller;
        private bool _started;
        private double _previousDistance = 0;
        private double _totalLapDistance;
        private double _lapDistance;
        #endregion

        /// <summary>
        /// Press button on keyboard to control software
        /// Added for driver convenience
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Enter))
            {
                if (_started)
                {
                    _started = false;
                    buttonStartStop.Text = "Start (enter)";
                }

                else
                {
                    _started = true;
                    buttonStartStop.Text = "Stop (enter)";
                }
            }

            if (keyData == (Keys.Space))
            {
                if (_started)
                {
                    _totalLapDistance = _carcontroller.GetTotalDistance();
                    _lapDistance = _totalLapDistance - _previousDistance;
                    listBoxLap.Items.Add(_lapDistance.ToString());
                    _previousDistance = _totalLapDistance;
                    //Autoscroll
                    listBoxLap.SelectedIndex = listBoxLap.Items.Count - 1;
                }
            }

            if (keyData == Keys.Escape)
            {
                this.Close();
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        #region Button Click Event Handler
        private void buttonNewLap_Click(object sender, EventArgs e)
        {
            if (_started)
            {
                _totalLapDistance = _carcontroller.GetTotalDistance();
                _lapDistance = _totalLapDistance - _previousDistance;
                listBoxLap.Items.Add(_lapDistance.ToString());
                _previousDistance = _totalLapDistance;
                //Autoscroll
                listBoxLap.SelectedIndex = listBoxLap.Items.Count - 1;
            }
        }

        private void buttonStartStop_Click(object sender, EventArgs e)
        {
            if (_started)
            {
                _started= false;
                buttonStartStop.Text = "Start (enter)";
            }

            else
            {
                _started = true;
                buttonStartStop.Text = "Stop (enter)";
            }
        }
        #endregion

        private void LapConfiguration_FormClosing(object sender, FormClosingEventArgs e)
        {
            listBoxLap.Items.Clear();
        }
    }
}
